package ah.html;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.StringReader;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.Vector;
import java.util.Map.Entry;

import javax.swing.text.html.HTMLEditorKit;

import file.FileCopy;

public class ParseHtmlTest {

	/**
	 * @author dingdahai
	 * @date Nov 18, 2009 9:52:17 AM
	 * @param args
	 */
	public static void main(String[] args) {
		// new ParseHtmlTest().testGetUrl();
			//new ParseHtmlTest().testParseJiBing();
		
	}

	/**
	 * test parse2 疾病页面
	 * 
	 * @author dingdahai
	 * @throws IOException
	 * @date Nov 18, 2009 5:11:27 PM
	 */
	public void testParseJiBing() throws IOException {
		HTMLEditorKit.Parser parser = new HtmlParser().getParser();// 实例化一个parser


		char ch = 'a';
		FileWriter fw = null;
		try {
			fw = new FileWriter("D:/39jk2/result.txt", false);
		} catch (IOException e1) {
			e1.printStackTrace();
			fw.close();
			throw e1;
		}
		try {
			for (; ch<='z'; ch++) {
				File dir = new File("D:/39jk2/"+ch);
				String[] files = dir.list();
				
				for (int n=0; n<files.length; n++) {
					Parser2 p = new Parser2("");
					parser.parse(new StringReader(FileCopy.readFileStr("D:/39jk2/"
							+ ch + "/" + files[n])), p, true);
					p.jiBing.setPinyinma((char)(ch-0x20)+"");
					fw.write(p.jiBing.toString());
					fw.write("\n");
				}

				
			}
		} catch (IOException e) {
			fw.close();
			e.printStackTrace();
		} finally {
			fw.close();
		}
	}

	public void testGetUrl() {
		HTMLEditorKit.Parser parser = new HtmlParser().getParser();// 实例化一个parser

		Parser p = new Parser("http://www.xjtu.edu.cn");

		try {
			parser.parse(new StringReader(GetHtml
					.getHtmlCode("http://www.xjtu.edu.cn")), p, true);// 所做的工作都为了这一句话！！！！！
		} catch (IOException e) {
			e.printStackTrace();
		}
		java.util.Vector link = p.getLinks();
		for (int i = 0; i < link.size(); i++) {
			System.out.println(link.get(i));
		}

	}

	/**
	 * 用hashMap保存，名字可能重复
	 * 
	 * @author dingdahai
	 * @date Nov 18, 2009 4:22:53 PM
	 */
	public void downloadUrl() {
		HTMLEditorKit.Parser parser = new HtmlParser().getParser();// 实例化一个parser
		/*
		 * 解析文件
		 */
		// 循环每个文件
		char c = 'a';
		int n = 0;
		for (int i = 0; i < 26; i++) {
			Parser p = new Parser(""); // 初始化回调类
			// 新建文件夹
			File dir = new File("D:/39jk/" + c);
			if (!dir.exists()) {
				dir.mkdir();
			}

			// 解析每个x.html
			try {
				parser.parse(new StringReader(FileCopy.readFileStr("D:/39jk/"
						+ c + ".html")), p, true);
			} catch (IOException e) {
				e.printStackTrace();
			}
			Map<String, String> linksMap = p.getLinksMap();
			Set<Entry<String, String>> linkEntrys = linksMap.entrySet();
			Iterator<Entry<String, String>> linkIterator = linkEntrys
					.iterator();
			// 遍历链接
			while (linkIterator.hasNext()) {
				// 给每个链接新建文件，保存内容
				Entry<String, String> entry = linkIterator.next();

				try {
					GetHtml.writeHtml("D:/39jk/" + c + "/" + entry.getKey()
							+ ".html", GetHtml.getHtmlCode(entry.getValue()),
							"NO");//
				} catch (RuntimeException e1) {
					System.out.println(n + "\thref=" + entry.getValue());
					e1.printStackTrace();
				}
			}

			c++;// 下一个文件
		}
	}

	/**
	 * 解析 下载链接 用数字作文件名
	 */
	public void downloadUrl2() {
		HTMLEditorKit.Parser parser = new HtmlParser().getParser();// 实例化一个parser

		/*
		 * 解析文件
		 */
		// 循环每个文件
		char c = 'a';
		int n = 0;
		for (int i = 0; i < 26; i++) {
			Parser p = new Parser(""); // 初始化回调类
			// 新建文件夹
			File dir = new File("D:/39jk2/" + c);
			if (!dir.exists()) {
				dir.mkdir();
			}

			// 解析每个x.html
			try {
				parser.parse(new StringReader(FileCopy.readFileStr("D:/39jk2/"
						+ c + ".html")), p, true);
			} catch (IOException e) {
				e.printStackTrace();
			}

			Vector<String> links = p.getLinks();
			Iterator<String> iterator = links.iterator();
			while (iterator.hasNext()) {
				String temp = iterator.next();
				try {
					GetHtml.writeHtml("D:/39jk2/" + c + "/" + n + ".html",
							GetHtml.getHtmlCode(temp), "NO");//
				} catch (RuntimeException e1) {
					System.out.println(n + "\thref=" + temp);
					e1.printStackTrace();
				}
				n++;
			}

			c++;// 下一个文件
		}
	}
}
